/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ps.server;

import com.ps.server.classes.products;
import java.util.ArrayList;
import java.util.Vector;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author Ricardo
 */
public class ProductLibraryXMLReader extends DefaultHandler {

    Vector nodos = new Vector();
    String contenido = "";
    String language = "";
    private ArrayList<langTemp> descriptionLanguage;
    private ArrayList categories;
    private ArrayList images;
    private ArrayList combinations;
    private ArrayList product_option_values;
    private ArrayList<pro_feature> product_features;
    private ArrayList tags;
    private boolean associations;
    private products nodoActualProducts = new products();
    String resource;
    private langTemp tem;
    private pro_feature pro_fea;
    private int parent;
    //private clsProduct nodoActual = new clsProduct();

    public ProductLibraryXMLReader(Vector nodos) {
        this.nodos = nodos;
    }

    public ProductLibraryXMLReader() {
        this.associations = false;
    }
    /*
     * Esta funcion el llamada cuando se produce el evento de ver una nueva
     * etiqueta
     */

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) {

        if ("product".equals(qName) && this.associations == false) {
            nodoActualProducts = new products();
        }

        if ("meta_description".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }
        if ("meta_keywords".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }
        if ("meta_title".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }
        if ("link_rewrite".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }
        if ("name".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }
        if ("available_now".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }
        if ("available_later".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }
        if ("description".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }
        if ("description_short".equals(qName)) {
            this.tem = new langTemp();
            this.descriptionLanguage = new ArrayList();
        }


        if ("language".equals(qName)) {
            this.tem.setId(attributes.getValue("id"));
            //this.short_description
            // this.idlang=attributes.getValue("id");
            //this.meta_keywords = new ArrayList();
        }
        if ("associations".equals(qName)) {
            this.associations = true;
        }
        if ("products".equals(qName)) {
            this.resource = qName;
        }

        if ("categories".equals(qName) && this.associations == true && this.categories == null && this.images == null && this.combinations == null && this.product_option_values == null && this.product_features == null && this.tags == null) {
            this.categories = new ArrayList();
        } else if ("images".equals(qName) && this.associations == true && this.categories != null && this.images == null && this.combinations == null && this.product_option_values == null && this.product_features == null && this.tags == null) {
            this.images = new ArrayList();
        } else if ("combinations".equals(qName) && this.associations == true && this.categories != null && this.images != null && this.combinations == null && this.product_option_values == null && this.product_features == null && this.tags == null) {
            this.combinations = new ArrayList();

        } else if ("product_option_values".equals(qName) && this.associations == true && this.categories != null && this.images != null && this.combinations != null && this.product_option_values == null && this.product_features == null && this.tags == null) {
            this.product_option_values = new ArrayList();
        } else if ("product_features".equals(qName) && this.associations == true && this.categories != null && this.images != null && this.combinations != null && this.product_option_values != null && this.product_features == null && this.tags == null) {
            this.product_features = new ArrayList();
        } else if ("tags".equals(qName) && this.associations == true && this.categories != null && this.images != null && this.combinations != null && this.product_option_values != null && this.product_features != null && this.tags == null) {
            this.tags = new ArrayList();
        }

        if ("product_feature".equals(qName)) {
            pro_fea = new pro_feature();
        }
        if ("combinations".equals(qName)) {
            parent = 0;
        }
    }

    /*
     * Esta funcion es llamada cuando ve el contenido de una etiqueta
     */
    @Override
    public void characters(char buf[], int offset, int len)
            throws SAXException {
        contenido = new String(buf, offset, len);

    }
    /*
     * y esta al llegar al final
     */

    @Override
    public void endElement(String uri, String localName, String qName) {

        if (this.resource.equals("products")) {
            resourceCategories(qName);
        }
    }

    void resourceCategories(String qName) {
        if ("id".equals(qName) && this.associations == false && this.categories == null && this.images == null) {
            // aqui se ya se sabe  el orden de  inicializacion de las variables.
            this.nodoActualProducts.setId(contenido);
        } else if ("id".equals(qName) && this.associations == true && this.categories != null && this.images == null && this.combinations == null && this.product_option_values == null && this.product_features == null && this.tags == null) {
            categories.add(contenido);
        } else if ("id".equals(qName) && this.associations == true && this.categories != null && this.images != null && this.combinations == null && this.product_option_values == null && this.product_features == null && this.tags == null) {
            this.images.add(contenido);
        } else if ("id".equals(qName) && this.associations == true && this.categories != null && this.images != null && this.combinations != null && this.product_option_values == null && this.product_features == null && this.tags == null) {
            this.combinations.add(contenido);
        } else if ("id".equals(qName) && this.associations == true && this.categories != null && this.images != null && this.combinations != null && this.product_option_values != null && this.product_features == null && this.tags == null) {
            this.product_option_values.add(contenido);
        } else if ("id".equals(qName) && this.associations == true && this.categories != null && this.images != null && this.combinations != null && this.product_option_values != null && this.product_features != null && this.tags == null) {
            this.pro_fea.setId(contenido);
            //this.product_features.add(contenido);
        } else if ("id".equals(qName) && this.associations == true && this.categories != null && this.images != null && this.combinations != null && this.product_option_values != null && this.product_features != null && this.tags != null) {
            this.tags.add(contenido);
        }

//            if (this.associations == true && this.categories != null && this.products == null ) {
//                categories.add(contenido);
//            } else if (this.associations == true && this.products != null) {
//                products.add(contenido);
//            } else {
//                nodoActualCategories.setId(contenido);
//            }
//        }
        if ("id_manufacturer".equals(qName)) {
            nodoActualProducts.setId_manufacturer(contenido);
        }
        if ("id_supplier".equals(qName)) {
            nodoActualProducts.setId_supplier(contenido);
        }
        if ("id_category_default".equals(qName)) {
            this.nodoActualProducts.setId_category_default(contenido);
        }

        if ("new".equals(qName)) {
            this.nodoActualProducts.setNewproduct(contenido);
        }
        if ("cache_default_attribute".equals(qName)) {
            this.nodoActualProducts.setCache_default_sttribute(contenido);
        }
        if ("id_default_image".equals(qName)) {
            this.nodoActualProducts.setId_default_image(contenido);
        }
        if ("id_default_combination".equals(qName)) {
            this.nodoActualProducts.setId_default_combination(contenido);
        }
        if ("id_tax_rules_group".equals(qName)) {
            this.nodoActualProducts.setId_tax_rules_group(contenido);
        }

        if ("minimal_quantity".equals(qName)) {
            this.nodoActualProducts.setMinimal_quantity(Integer.valueOf(contenido));
        }
        if ("price".equals(qName)) {
            this.nodoActualProducts.setPrice(Double.valueOf(contenido));
        }

        if ("additional_shipping_cost".equals(qName)) {
            this.nodoActualProducts.setAdditional_shipping_cost(Double.valueOf(contenido));
        }

        if ("wholesale_price".equals(qName)) {
            this.nodoActualProducts.setWholesale_price(Double.valueOf(contenido));
        }
        if ("on_sale".equals(qName)) {
            this.nodoActualProducts.setOn_sale(Boolean.valueOf(contenido));
        }
        if ("online_only".equals(qName)) {
            this.nodoActualProducts.setOnline_only(Boolean.valueOf(contenido));
        }
        if ("ecotax".equals(qName)) {
            this.nodoActualProducts.setEcotax(Double.valueOf(contenido));
        }
        if ("unity".equals(qName)) {
            if (contenido.trim().equals("")) {
                contenido = "0";
            }
            this.nodoActualProducts.setUnity(contenido);
        }
        if ("unit_price_ratio".equals(qName)) {
            this.nodoActualProducts.setUnit_price_ratio(Double.valueOf(contenido));
        }
        if ("reference".equals(qName)) {
            this.nodoActualProducts.setReference(contenido);
        }
        if ("supplier_reference".equals(qName)) {
            this.nodoActualProducts.setSupplier_reference(contenido);
        }
        if ("location".equals(qName)) {
            this.nodoActualProducts.setLocation(contenido);
        }
        if ("width".equals(qName)) {
            this.nodoActualProducts.setWidth(Double.valueOf(contenido));
        }
        if ("height".equals(qName)) {
            this.nodoActualProducts.setHeigth(Double.valueOf(contenido));
        }
        if ("depth".equals(qName)) {
            this.nodoActualProducts.setDepth(Double.valueOf(contenido));
        }
        if ("weight".equals(qName)) {
            this.nodoActualProducts.setWeight(Double.valueOf(contenido));
        }
        if ("quantity_discount".equals(qName)) {
            this.nodoActualProducts.setQuantity_discount(Boolean.valueOf(contenido));
        }
        if ("customizable".equals(qName)) {
            this.nodoActualProducts.setCustomizable(contenido);
        }
        if ("uploadable_files".equals(qName)) {
            this.nodoActualProducts.setUploadable_files(contenido);
        }
        if ("text_fields".equals(qName)) {
            this.nodoActualProducts.setText_fields(contenido);
        }
        if ("active".equals(qName)) {
            this.nodoActualProducts.setActive(Boolean.valueOf(contenido));
        }
        if ("available_for_order".equals(qName)) {
            this.nodoActualProducts.setAvailable_for_order(Boolean.valueOf(contenido));
        }
        if ("available_date".equals(qName)) {
            this.nodoActualProducts.setAvailable_date(contenido);
        }
        if ("condition".equals(qName)) {
            this.nodoActualProducts.setCondition(contenido);
        }
        if ("show_price".equals(qName)) {
            this.nodoActualProducts.setShow_price(Boolean.valueOf(contenido));
        }
        if ("ean13".equals(qName)) {
            this.nodoActualProducts.setEan13(contenido);
        }
        if ("upc".equals(qName)) {
            this.nodoActualProducts.setUpc(contenido);
        }
        if ("indexed".equals(qName)) {
            this.nodoActualProducts.setIndexed(Boolean.valueOf(contenido));
        }
        if ("visibility".equals(qName)) {
            this.nodoActualProducts.setVisibility(contenido);
        }
        if ("cache_is_pack".equals(qName)) {
            this.nodoActualProducts.setCache_is_pack(Boolean.valueOf(contenido));
        }
        if ("is_virtual".equals(qName)) {
            this.nodoActualProducts.setIs_virtual(Boolean.valueOf(contenido));
        }

        if ("cache_has_attachments".equals(qName)) {
            this.nodoActualProducts.setCache_has_attachments(Boolean.valueOf(contenido));
        }

        if ("date_add".equals(qName)) {
            this.nodoActualProducts.setDate_add(contenido);
        }
        if ("date_upd".equals(qName)) {
            this.nodoActualProducts.setDate_upd(contenido);
        }
        if ("advanced_stock_management".equals(qName)) {
            this.nodoActualProducts.setAdvanced_stock_management(Boolean.valueOf(contenido));
        }

        if ("meta_description".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualProducts.setMeta_description(shortString);
//            nodoActualCategories.setName(contenido);
        }

        if ("meta_keywords".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualProducts.setMeta_keywords(shortString);
        }
        if ("meta_title".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualProducts.setMeta_title(shortString);
        }
        if ("link_rewrite".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualProducts.setLink_rewrite(shortString);
        }
        if ("name".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualProducts.setName(shortString);
        }

        if ("available_now".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualProducts.setAvailable_now(shortString);
        }

        if ("available_later".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualProducts.setAvailable_later(shortString);
        }

        if ("description".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualProducts.setDescription(shortString);
        }
        if ("description_short".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualProducts.setDescription_short(shortString);
        }

        if ("language".equals(qName)) {
            langTemp t = new langTemp();
            t.setId(this.tem.getId());
            t.setValue(contenido);
            this.descriptionLanguage.add(t);
            //nodoActualManufacturers.setId(contenido);
        }

        if ("categories".equals(qName)) {
            if (this.associations == true) {
                String[] shortString = new String[this.categories.size()];
                for (int x = 0; this.categories.size() > x; x++) {
                    shortString[x] = this.categories.get(x).toString();
                    // shortString[x][0]=this.idlang;
                }
                nodoActualProducts.setCategories(shortString);
            }
        }
        if ("images".equals(qName)) {
            if (this.associations == true) {
                String[] shortString = new String[this.images.size()];
                for (int x = 0; this.images.size() > x; x++) {
                    shortString[x] = this.images.get(x).toString();
                    // shortString[x][0]=this.idlang;
                }
                nodoActualProducts.setImages(shortString);
            }
        }
        if ("combinations".equals(qName)) {
            if (parent >= 0) {
                parent++;
            }
            if (parent == 2 && this.associations == true) {
                String[] shortString = new String[this.combinations.size()];
                for (int x = 0; this.combinations.size() > x; x++) {
                    shortString[x] = this.combinations.get(x).toString();
                    // shortString[x][0]=this.idlang;
                }
                nodoActualProducts.setCombinations(shortString);
            }
        }
        if ("product_option_values".equals(qName)) {
            if (this.associations == true) {
                String[] shortString = new String[this.product_option_values.size()];
                for (int x = 0; this.product_option_values.size() > x; x++) {
                    shortString[x] = this.product_option_values.get(x).toString();
                    // shortString[x][0]=this.idlang;
                }
                nodoActualProducts.setProduct_option_values(shortString);
            }
        }

        if ("id_feature_value".equals(qName)) {
            this.pro_fea.setId_feature_value(contenido);
        }

        if ("product_feature".equals(qName)) {
            this.product_features.add(pro_fea);
        }

        if ("product_features".equals(qName)) {
            String[][] shortString = new String[this.product_features.size()][2];
            for (int x = 0; this.product_features.size() > x; x++) {
                shortString[x][0] = this.product_features.get(x).getId();
                shortString[x][1] = this.product_features.get(x).getId_feature_value();
                // shortString[x][0]=this.idlang;
            }
            nodoActualProducts.setProduct_features(shortString);
        }
        if ("tags".equals(qName)) {
            if (this.associations == true) {
                String[] shortString = new String[this.tags.size()];
                for (int x = 0; this.tags.size() > x; x++) {
                    shortString[x] = this.tags.get(x).toString();
                    // shortString[x][0]=this.idlang;
                }
                nodoActualProducts.setTags(shortString);
            }
        }
        if ("product".equals(qName) && this.associations == false && this.categories != null && this.images != null) {
            this.nodos.add(this.nodoActualProducts);



            this.categories = null;
            this.images = null;
            this.combinations = null;
            this.product_option_values = null;
            this.product_features = null;
            this.tags = null;
        }
//            if (this.categories != null && this.products != null) {
//
//                this.nodos.add(nodoActualCategories);
//                this.contenido = this.nodoActualCategories.toXML();
//            }
        //this.associations = false;
        //nodoActualManufacturers.setId(contenido);
        //}
        if ("associations".equals(qName)) {
            this.associations = false;


        }

    }

    class langTemp {

        private String id;
        private String value;

        public void langTemp() {
        }

        public void langTemp(String id, String value) {
            this.setId(id);
            this.setValue(value);
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getValue() {
            if (value == null) {
                value = "";
            }
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

    class pro_feature {

        private String id;
        private String id_feature_value;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getId_feature_value() {
            return id_feature_value;
        }

        public void setId_feature_value(String id_feature_value) {
            this.id_feature_value = id_feature_value;
        }
    }
}
